package com.ai.bitcoin.controller;

import com.ai.bitcoin.annotation.Privilege;
import com.ai.bitcoin.constant.CodeConstant;
import com.ai.bitcoin.input.GeneralRequest;
import com.ai.bitcoin.input.RequestHepler;
import com.ai.bitcoin.jpa.dao.BccUserDao;
import com.ai.bitcoin.jpa.entity.BccUser;
import com.ai.bitcoin.output.GeneralReponse;
import com.ai.bitcoin.service.ExtraRedeemCodeService;
import com.ai.bitcoin.service.helper.ExtraRedeemServiceHelper;
import com.ai.bitcoin.util.JacksonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;


@RequestMapping("/extra-redeem-code")
@Slf4j
@RestController
public class ExtraRedeemController {

    @Autowired
    private ExtraRedeemCodeService extraRedeemCodeService;

    @Autowired
    private ExtraRedeemServiceHelper extraRedeemServiceHelper;

    /**
     * 从excel导入兑换码
     * @param
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String exchange(@RequestParam("file") MultipartFile file) {
        int i = 0;
        try {
            i = extraRedeemServiceHelper.improtFromExcel(file);
        }catch (Exception e){
            log.error("导入数据失败！！");
            return JacksonUtils.toJson(new GeneralReponse(CodeConstant.BUSINESS_ERROR,null,"数据导入失败！！"));
        }
        return JacksonUtils.toJson(new GeneralReponse(CodeConstant.SUCCESS,i));
    }


    /**
     * 获取额外的兑换码
     * @param request
     * @return
     */
    @Privilege
    @RequestMapping(value = "/getOne", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public String exchange(HttpServletRequest request) {
        GeneralRequest generalRequest = RequestHepler.processRequest(request);
        BccUser user = generalRequest.getUser();
        return JacksonUtils.toJson(extraRedeemCodeService.getOne(user));
    }

}
